package activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.jaden.washeradministrator.*;
import com.example.jaden.washeradministrator.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;

public class RegisterPage extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    EditText setloginName;
    EditText setPassword;
    EditText confirmPassword;
    Button confirm;
    EditText setAddress;
    EditText setcitizenName;
    EditText setPhone;
    RadioGroup sexSelect;
    RadioButton sexmale;
    RadioButton sexfemale;
    MyApplication application;
    int sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registerpage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        Toolbar toolbar=(Toolbar)findViewById(R.id.homepage_toolbar);
        setSupportActionBar(toolbar);
        //获取控件实例
        getInstance();
        //执行按键点击监听
        setClickListener();


    }

    //获取控件实例
    private void getInstance() {
        confirm=(Button)findViewById(R.id.button_confirm_registerpage);
        setloginName =(EditText)findViewById(R.id.editText_username_registerpage);
        setPassword=(EditText)findViewById(R.id.editText_password_registerpage);
        confirmPassword=(EditText)findViewById(R.id.editText_password2_registerpage);
        setAddress=(EditText)findViewById(R.id.editText_address_registerpage);
        setcitizenName=(EditText)findViewById(R.id.editText_citizenName_registerpage);
        setPhone=(EditText)findViewById(R.id.editText_phone_registerpage);
        sexSelect=(RadioGroup)findViewById(R.id.radioGroup_sex_registerpage);
        sexmale=(RadioButton)findViewById(R.id.radioButton_sexmale_registerpage);
        sexfemale=(RadioButton)findViewById(R.id.radioButton_sexfemale_registerpage);
        application=MyApplication.getInstance();
    }
    //执行按键点击监听
    private void setClickListener(){
        confirm.setOnClickListener(this);
        sexmale.setOnCheckedChangeListener(this);
        sexfemale.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button_confirm_registerpage:
                confirm.setEnabled(false);
                String username= setloginName.getText().toString().trim();
                String password=setPassword.getText().toString().trim();
                String confirmerpassword=confirmPassword.getText().toString().trim();
                String citizenname=setcitizenName.getText().toString().trim();
                String citizenaddress=setAddress.getText().toString().trim();
                String citizenphone=setPhone.getText().toString().trim();
                if(!password.equals(confirmerpassword))
                {
                    Toast.makeText(RegisterPage.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                    setPassword.setText("");
                    confirmPassword.setText("");

                }
                else {
                    String url="http://47.96.159.236:8080/Wash/Login?method=Register";
                    //打包Json数据
                    JSONObject userJson=new JSONObject();
                    try {
                        userJson.put("loginName",username);
                        userJson.put("password",password);
                        userJson.put("userIdentity",2);
                        userJson.put("citizenUsername",citizenname);
                        userJson.put("citizenGender",sex);
                        userJson.put("citizenAddress",citizenaddress);
                        userJson.put("citizenPhone",citizenphone);
                        String messageJson=String.valueOf(userJson);
                        TransferData.Register(url,messageJson);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                    while (true) {
                        boolean isOK=application.isRegisterOk();
                        if(isOK) {
                            application.setRegisterOk(false);
                            ResponseMessage responseMessage=application.getResponseMessage();
                            verification(responseMessage);
                        }
                    }
                }
                break;
        }
    }
    public void verification(ResponseMessage responseMessage) {
        switch (responseMessage.getCode()) {
            case 1001:
                Toast.makeText(RegisterPage.this, "注册成功", Toast.LENGTH_LONG).show();
                onDestroy();
                //Intent intent=new Intent(this,loginPage.class);
                //startActivity(intent);
                break;
                default:
                    Toast.makeText(RegisterPage.this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                    break;
        }
    }

    @Override
    protected void onResume() {
        //当从上一个活动按下BACK键以后，返回当前注册页面，确认按钮可用
        confirm.setEnabled(true);
        super.onResume();

    }

    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();

    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId())
        {
            case R.id.radioButton_sexmale_registerpage:
                if(isChecked)
                {
                        sex=1;
                }
                break;
            case R.id.radioButton_sexfemale_registerpage:
                if(isChecked)
                {
                        sex=0;
                }
                break;
        }
    }
}
